package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportRolesRpcService;
import com.hunttown.mes.rpc.domain.TransportRolesDTO;
import com.hunttown.mes.rpc.domain.query.TransportRolesDTOQuery;
import com.hunttown.mes.service.TransportRolesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportRolesConverts.convert;

/**
 * created by wangjunfu on 2022-01-28 11:17:47
 */
@Service
public class TransportRolesRpcServiceImpl implements TransportRolesRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportRolesRpcServiceImpl.class);

    private final TransportRolesService transportRolesService;

    @Autowired
    public TransportRolesRpcServiceImpl(TransportRolesService transportRolesService) {
        this.transportRolesService = transportRolesService;
    }

    @Override
    public TransportRolesDTO insert(TransportRolesDTO transportRolesDTO) {
        try {
            return convert(transportRolesService.insert(convert(transportRolesDTO)));
        } catch (Exception e) {
            logger.error("TransportRoles create failed.", e);
            throw new RuntimeException("TransportRoles create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportRolesDTO transportRolesDTO) {
        try {
            return transportRolesService.updateInfoById(convert(transportRolesDTO));
        } catch (Exception e) {
            logger.error("TransportRoles update by id failed.", e);
            throw new RuntimeException("TransportRoles update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportRolesDTOQuery transportRolesDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportRolesDTOQuery.getW_id() != null && transportRolesDTOQuery.getW_id() > 0) {
                query.put("w_id", transportRolesDTOQuery.getW_id());
            }

            if (transportRolesDTOQuery.getIds() != null && !transportRolesDTOQuery.getIds().equals("")) {
                query.put("ids", transportRolesDTOQuery.getIds());
            }

            if (transportRolesDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportRolesDTOQuery.getStopFlag());
            }

            if (transportRolesDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportRolesDTOQuery.getDeleteFlag());
            }

            return transportRolesService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportRoles update by query failed.", e);
            throw new RuntimeException("TransportRoles update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportRolesDTO selectById(Integer id) {
        try {
            return convert(transportRolesService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportRoles select by id failed.", e);
            throw new RuntimeException("TransportRoles select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportRolesDTO selectObjByQuery(TransportRolesDTOQuery transportRolesDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(transportRolesDTOQuery.getRoleName())) {
                query.put("roleName", transportRolesDTOQuery.getRoleName());
            }

            if (transportRolesDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportRolesDTOQuery.getStopFlag());
            }

            if (transportRolesDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportRolesDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportRolesDTOQuery.getOrderBy())) {
                query.put("orderBy", transportRolesDTOQuery.getOrderBy());
            }

            return convert(transportRolesService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportRoles select obj by query failed.", e);
            throw new RuntimeException("TransportRoles select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportRolesDTO> getListForPage(TransportRolesDTOQuery transportRolesDTOQuery) {
        Query query = Query.page(transportRolesDTOQuery.getPn(), transportRolesDTOQuery.getPageSize());

        if (transportRolesDTOQuery.getId() != null && transportRolesDTOQuery.getId() > 0) {
            query.put("id", transportRolesDTOQuery.getId());
        }

        if (transportRolesDTOQuery.getIds() != null && !transportRolesDTOQuery.getIds().equals("")) {
            query.put("ids", transportRolesDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportRolesDTOQuery.getLikeName())) {
            query.put("likeName", transportRolesDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportRolesDTOQuery.getShowDate())) {
            query.put("showDate", transportRolesDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportRolesDTOQuery.getStartDate())) {
            query.put("startDate", transportRolesDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportRolesDTOQuery.getEndDate())) {
            query.put("endDate", transportRolesDTOQuery.getEndDate());
        }

        if (transportRolesDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportRolesDTOQuery.getStopFlag());
        }

        if (transportRolesDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportRolesDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(transportRolesDTOQuery.getOrderBy())) {
            query.put("orderBy", transportRolesDTOQuery.getOrderBy());
        }

        List<TransportRolesDTO> list = convert(transportRolesService.selectByQuery(query));
        Page<TransportRolesDTO> page = Page.from(transportRolesDTOQuery.getPn(), transportRolesDTOQuery.getPageSize(), list);
        page.setTotalItems(transportRolesService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportRolesService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportRoles delete failed.", e);
            throw new RuntimeException("TransportRoles delete failed, Please order your code.", e);
        }
    }
}